﻿package com.webbei.shoppingCar.action;

import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class SessionCheckInterceptor extends AbstractInterceptor {
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ac = invocation.getInvocationContext();
		String parm = (String) ac.getParameters().get("withoutAuthentication");
		System.out.println("action中的parm：" + parm);
		boolean excluded = false;
		if (parm != null) {
			if (parm.toUpperCase().equals("TRUE")) {
				excluded = true;
			}
		}
		System.out.println("intercept不检查的Action:" + excluded);
		String user = (String) ac.getSession().get("username");
		System.out.println("intercept有session:" + user);

		boolean hasCookies = false;
		Cookie[] cookies = ServletActionContext.getRequest().getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("wangbeiCookieUsername")&& !cookie.getValue() .equals("")  && cookie.getValue() != null) {
					System.out.println("cookies Value："+cookie.getValue());
					hasCookies = true;
				}
			}
		}
		System.out.println("intercept有cookies:" + hasCookies);
		System.out.println(excluded || user != null || hasCookies);
		if (excluded || user != null || hasCookies) {
			System.out.println("通过");
			return invocation.invoke();
		}
		// ac.put("tip", "您还没有登录!");
		return "login";
	}
}